// pages/main/main.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    tishi: '',
    wendu: '',
    types: '',
    fengxiang: '',
    fengli: '',
    future: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadInfo()
  },

  loadInfo:function(){
    var that = this
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        console.log(latitude,longitude)
        that.loadCity(latitude, longitude)
      }
    })
  },
  loadCity: function (latitude, longitude){
    var that = this
    wx.request({
      url: 'http://api.map.baidu.com/geocoder/v2/?ak=D6WOzHaymzVVKvgiy8UbhQEznkgeK6BD&location=' + latitude + ',' + longitude + '&output=json',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data.result.addressComponent.city)
        var city = res.data.result.addressComponent.city
        city = city.replace('市', '')
        that.setData({
          city: city
        });
        that.loadWeather(city)
      }
    })
  },
  loadWeather:function(city){
    var that = this
    wx.request({
      url: 'http://wthrcdn.etouch.cn/weather_mini?city=' + city,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        var future = res.data.data.forecast
        future.shift()
        console.log(future)
        that.setData({
          tishi: res.data.data.ganmao,
          wendu: res.data.data.wendu,
          types: res.data.data.forecast[0].type,
          fengxiang: res.data.data.forecast[0].fengxiang,
          fengli: res.data.data.forecast[0].fengli.replace('<![CDATA[<','').replace(']]>',''),
          future: future
        })
      }
    })
  }

})